/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.irssproject.app.entitats;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author adria
 */
@Entity
@Table(name = "lectures")
@NamedQueries({
    @NamedQuery(name = "Reading.findAll", query = "SELECT l FROM Reading l")})
public class Reading implements Serializable, Comparable<Object> {

    private static final long serialVersionUID = 1L;
    private Long id;
    private Boolean read;
    private Boolean liked;
    private float readingTime;
    private Integer relevance;
    private Date addedDate;
    private Subscription subscription;
    private Item item;

    public Reading() {
    }

    public Reading(Long idlectura) {
        this.id = idlectura;
    }

    public Reading(Long idlectura, Boolean llegida, Boolean valorada, float temps, Integer rellevancia, Date dataAfegida) {
        this.id = idlectura;
        this.read = llegida;
        this.liked = valorada;
        this.readingTime = temps;
        this.relevance = rellevancia;
        this.addedDate = dataAfegida;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idlectura")
    public Long getId() {
        return id;
    }

    public void setId(Long idlectura) {
        this.id = idlectura;
    }

    @Basic(optional = false)
    @Column(name = "llegida")
    public Boolean isRead() {
        return read;
    }

    public void setRead(Boolean llegida) {
        this.read = llegida;
    }

    @Basic(optional = false)
    @Column(name = "valorada")
    public Boolean isLiked() {
        return liked;
    }

    public void setLiked(Boolean valorada) {
        this.liked = valorada;
    }

    @Basic(optional = false)
    @Column(name = "temps")
    public float getReadingTime() {
        return readingTime;
    }

    public void setReadingTime(float temps) {
        this.readingTime = temps;
    }

    @Basic(optional = false)
    @Column(name = "rellevancia")
    public Integer getRelevance() {
        return relevance;
    }

    public void setRelevance(Integer rellevancia) {
        this.relevance = rellevancia;
    }

    @Basic(optional = false)
    @Column(name = "data_afegida", insertable = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(Date dataAfegida) {
        this.addedDate = dataAfegida;
    }

    @JoinColumn(name = "idsubscripcio", referencedColumnName = "idsubscripcio")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Subscription getSubscription() {
        return subscription;
    }

    public void setSubscription(Subscription subscripcio) {
        this.subscription = subscripcio;
    }

    @JoinColumn(name = "iditem", referencedColumnName = "iditem")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Reading)) {
            return false;
        }
        Reading other = (Reading) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.irssproject.app.entitats.Lectura[idlectura=" + id + "]";
    }

    @Override
    public int compareTo(Object o) {

        int compareResult = this.item.getPublishedDate().compareTo(((Reading) o).getItem().getPublishedDate());

        if (compareResult == 0) {
            return 0;
        } else if (compareResult > 0) {
            return -1;
        } else {
            return 1;
        }
    }

}
